package com.ysd.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.entity.Consume_log;
import com.ysd.repository.Consume_logRepository;
import com.ysd.util.Result;

@RestController
public class Consume_logController {
	@Autowired
	private Consume_logRepository consume_logRepository;
	
	//进入资源室(添加)
	@RequestMapping("addConsume_log")
	public Object addConsume_log(Consume_log c) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		c.setIn_time(df.format(new Date()));
		Consume_log cl = consume_logRepository.save(c);
		if (cl!=null) {
			return Result.toClient("0", "添加成功", cl);
		} else {
			return Result.toClient("1", "添加失败", null);
		}
	}
	
	//修改状态(离开资源室)
	@RequestMapping("updateConsume_logById")
	public Object updateConsume_logById(Integer id,String out_time) {
			
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String outTime=df.format(new Date());
			
		try {
			consume_logRepository.updateConsume_logById(id, outTime);
			return Result.toClient("0","离开资源室成功",null);
		} catch (Exception e) {
			return Result.toClient("1","离开资源室失败",null);
		}
	}
	
	//通过资源室id和学号同时查询查询
	@RequestMapping("selByMemberAndRead_room")
	public Object selByMemberAndRead_room(String read_room_id,String card_no) {
		System.out.println("资源室"+read_room_id);
		System.out.println("学号"+card_no);
		Consume_log c = consume_logRepository.findByMemberAndRead_room(read_room_id, card_no);
		System.out.println(c);
		if (c != null) {
			return Result.toClient("0", "查询成功", c);
		} else {
			return Result.toClient("1", "查询失败", null);
		}
	}
	
}
